package com.Managers;


import java.io.File;
import android.content.Context;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;


public class DownloadManagerClass {

    public static ImageLoaderConfiguration set(Context context) {

        DisplayImageOptions.Builder options = new DisplayImageOptions.Builder();

        options.cacheInMemory(false);
        options.cacheOnDisc(true);
        options.considerExifParams(true);
        options.delayBeforeLoading(100);
        options.imageScaleType(ImageScaleType.IN_SAMPLE_INT);
        options.bitmapConfig(Bitmap.Config.RGB_565);

        File cacheDir = StorageUtils.getOwnCacheDirectory(context, DirManagerClass.ThumbsPath);
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.discCache(new UnlimitedDiscCache(cacheDir, new FileNameGenerator() {

            @Override
            public String generate(String imageUri) {

                return makePathPost(imageUri);
            }
        }));

        config.threadPoolSize(1);

        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.defaultDisplayImageOptions(options.build());
        return config.build();
    }

    public static ImageLoaderConfiguration setforAlbum(Context context) {

        DisplayImageOptions.Builder options = new DisplayImageOptions.Builder();

        options.cacheOnDisc(true);
        options.delayBeforeLoading(100);
        options.imageScaleType(ImageScaleType.IN_SAMPLE_INT);
        options.bitmapConfig(Bitmap.Config.RGB_565);

        File cacheDir = StorageUtils.getOwnCacheDirectory(context, DirManagerClass.TempPath);
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.discCache(new UnlimitedDiscCache(cacheDir, new FileNameGenerator() {

            @Override
            public String generate(String imageUri) {

                return makePathPost(imageUri);
            }
        }));
        config.threadPoolSize(3);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.tasksProcessingOrder(QueueProcessingType.FIFO);
        config.defaultDisplayImageOptions(options.build());

        return config.build();
    }

    public static ImageLoaderConfiguration setforUpload(Context context) {

        DisplayImageOptions.Builder options = new DisplayImageOptions.Builder();

        options.cacheInMemory(true);
        options.cacheOnDisc(true);
        options.delayBeforeLoading(100);
        options.imageScaleType(ImageScaleType.IN_SAMPLE_INT);
        options.bitmapConfig(Bitmap.Config.RGB_565);

        File cacheDir = StorageUtils.getOwnCacheDirectory(context, DirManagerClass.UpPath);
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.discCache(new UnlimitedDiscCache(cacheDir, new FileNameGenerator() {

            @Override
            public String generate(String imageUri) {

                return makePathPost(imageUri);
            }
        }));

        config.memoryCacheExtraOptions(200, 200);
        config.threadPoolSize(3);
        config.threadPriority(Thread.NORM_PRIORITY - 1);
        config.memoryCacheSize(2 * 1024 * 1024);
        config.memoryCacheSizePercentage(13);
        config.discCacheSize(50 * 1024 * 1024);
        config.discCacheFileCount(100);
        config.denyCacheImageMultipleSizesInMemory();
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.defaultDisplayImageOptions(options.build());
        return config.build();
    }

    private static String makePathPost(String iTag) {

        // String fullSdPath = "";
        String withoutExt = "";
        //
        String[] sp = iTag.split("/");
        withoutExt = sp[sp.length - 1];

        // if (downloadProfile) {
        // fullSdPath = Util.SDPATH + Util.PROPATH + withOutExt;
        // }
        // else {
        // fullSdPath = DirManagerClass.SDPATH + DirManagerClass.DIRPATH +
        // withOutExt;
        // }

        return withoutExt;
    }
}
